package vn.timsua.utils;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;

public class Util {
    public Util() {
    }

    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
                //System.out.println("Close connection... successful.");
            }
        } catch (SQLException e) {
            System.out.println("Close connection... error.");
        }
    }

    public static void closeConnection(PreparedStatement pstmt) {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {}
    }

    public static void closeConnection(Statement stmt) {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {}
    }

    public static void closeConnection(PreparedStatement pstmt, Connection conn) {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {}
        try {
            if (conn != null) {
                conn.close();
                //System.out.println("Close connection... successful.");
            }
        } catch (SQLException e) {
            System.out.println("Close connection... error.");
        }
    }

    public static void closeConnection(Statement stmt, Connection conn) {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {}
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            System.out.println("Close connection... error.");
        }
    }

    public static void closeConnection(ResultSet rs, PreparedStatement pstmt) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {}
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {}
    }

    public static void closeConnection(ResultSet rs, Statement stmt) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {}
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {}
    }

    public static void closeConnection(ResultSet rs, PreparedStatement pstmt,
                                       Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {}
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {}
        try {
            if (conn != null) {
                conn.close();
                //System.out.println("Close connection... successful.");
            }
        } catch (SQLException e) {
            System.out.println("Close connection... error.");
        }
    }

    public static void closeConnection(ResultSet rs, Statement stmt,
                                       Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {}
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {}
        try {
            if (conn != null) {
                conn.close();
                //System.out.println("Close connection... successful.");
            }
        } catch (SQLException e) {
            System.out.println("Close connection... error.");
        }
    }

    public static void SetAutoCommit(Connection conn, boolean bAuto) {
        try {
            conn.setAutoCommit(bAuto);
        } catch (Exception ex) {
            System.out.println("Loi setAutoCommit.");
            ex.printStackTrace();
        }
    }

    public static void Commit(Connection conn) {
        try {
            conn.commit();
        } catch (Exception ex) {
            System.out.println("Loi commit.");
            ex.printStackTrace();
        }
    }

    public static void Rollback(Connection conn) {
        try {
            conn.rollback();
        } catch (Exception ex) {
            System.out.println("Loi rollback.");
            ex.printStackTrace();
        }
    }
}
